package com.carey.blog.action.feed;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.carey.blog.framework.latke.Keys;
import com.carey.blog.framework.latke.model.User;
import com.carey.blog.framework.latke.repository.FilterOperator;
import com.carey.blog.framework.latke.repository.Query;
import com.carey.blog.framework.latke.repository.SortDirection;
import com.carey.blog.model.Article;
import com.carey.blog.model.Preference;
import com.carey.blog.repository.ArticleRepository;
import com.carey.blog.repository.impl.ArticleGAERepository;
import com.carey.blog.util.Articles;
import com.carey.blog.util.Preferences;
import com.carey.blog.util.TimeZones;

/**
 * Blog articles feed.
 * 
 */
public final class BlogArticlesFeedServlet extends HttpServlet {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Article repository.
	 */
	private ArticleRepository articleRepository = ArticleGAERepository
			.getInstance();
	/**
	 * Preference utilities.
	 */
	private Preferences preferenceUtils = Preferences.getInstance();
	/**
	 * Count of output entry.
	 */
	public static final int ENTRY_OUTPUT_CNT = 10;
	/**
	 * Article utilities.
	 */
	private Articles articleUtils = Articles.getInstance();
	/**
	 * Time zone utilities.
	 */
	private TimeZones timeZoneUtils = TimeZones.getInstance();

	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		response.setContentType("application/atom+xml");
		response.setCharacterEncoding("UTF-8");

		final Feed feed = new Feed();
		try {
			final JSONObject preference = preferenceUtils.getPreference();
			if (null == preference) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
				return;
			}

			final String blogTitle = preference
					.getString(Preference.BLOG_TITLE);
			final String blogSubtitle = preference
					.getString(Preference.BLOG_SUBTITLE);
			final String blogHost = preference.getString(Preference.BLOG_HOST);

			feed.setTitle(StringEscapeUtils.escapeXml(blogTitle));
			feed.setSubtitle(StringEscapeUtils.escapeXml(blogSubtitle));
			feed.setUpdated(timeZoneUtils.getTime(preference
					.getString(Preference.TIME_ZONE_ID)));
			feed.setAuthor(StringEscapeUtils.escapeXml(blogTitle));
			feed.setLink("http://" + blogHost);

			final Query query = new Query()
					.setCurrentPageNum(1)
					.setPageSize(ENTRY_OUTPUT_CNT)
					.addFilter(Article.ARTICLE_IS_PUBLISHED,
							FilterOperator.EQUAL, true)
					.addSort(Article.ARTICLE_CREATE_DATE,
							SortDirection.DESCENDING);

			final JSONObject articleResult = articleRepository.get(query);
			final JSONArray articles = articleResult.getJSONArray(Keys.RESULTS);
			for (int i = 0; i < articles.length(); i++) {
				final JSONObject article = articles.getJSONObject(i);
				final Entry entry = new Entry();
				feed.addEntry(entry);
				final String title = StringEscapeUtils.escapeXml(article
						.getString(Article.ARTICLE_TITLE));
				entry.setTitle(title);
				final String summary = StringEscapeUtils.escapeXml(article
						.getString(Article.ARTICLE_ABSTRACT));
				entry.setSummary(summary);
				final Date updated = (Date) article
						.get(Article.ARTICLE_UPDATE_DATE);
				entry.setUpdated(updated);
				final String id = article.getString(Keys.OBJECT_ID);
				entry.setId(id);
				final String link = "http://" + blogHost
						+ article.getString(Article.ARTICLE_PERMALINK);
				entry.setLink(link);
				final String authorName = StringEscapeUtils
						.escapeXml(articleUtils.getAuthor(article).getString(
								User.USER_NAME));
				entry.setAuthor(authorName);

				final String tagsString = article
						.getString(Article.ARTICLE_TAGS_REF);
				final String[] tagStrings = tagsString.split(",");
				for (int j = 0; j < tagStrings.length; j++) {
					final Category catetory = new Category();
					entry.addCatetory(catetory);
					final String tag = tagStrings[j];
					catetory.setTerm(tag);
				}
			}

			final PrintWriter writer = response.getWriter();
			writer.write(feed.toString());
			writer.close();
		} catch (final Exception e) {
			throw new IOException(e);
		}
	}
}
